package dao

import (
	"TTMS/model"
	"TTMS/utils"
)

// AddSchedule 添加演出计划
func AddSchedule(schedule *model.Schedule) error {
	//写 sql 语句
	sqlStr := "insert into schedules(hall_id, film_name, date, start_time, end_time, price) values (?,?,?,?,?,?)"
	//执行
	res, err := utils.DB.Exec(sqlStr, schedule.HallID, schedule.FilmName, schedule.Date, schedule.StartTime, schedule.EndTime, schedule.Price)
	if err != nil {
		return err
	}
	//获取演出计划的ID
	id, err := res.LastInsertId()
	if err != nil {
		return err
	}
	//根据演出厅ID获取演出厅
	hall := GetHallByID(schedule.HallID)
	AddSeats(hall, id)
	return nil
}

// UpdateSchedule 修改演出计划
func UpdateSchedule(schedule *model.Schedule) error {
	//写 sql 语句
	sqlStr := "update schedules set hall_id=?, film_name=?, date=?, start_time=?, end_time=?, price=? where id=?"
	//执行
	_, err := utils.DB.Exec(sqlStr, schedule.HallID, schedule.FilmName, schedule.Date, schedule.StartTime, schedule.EndTime, schedule.Price, schedule.ID)
	if err != nil {
		return err
	}
	//更新座位的数据
	//1.删除之前的座位数据
	err = DeleteSeats(schedule.ID)
	if err != nil {
		return err
	}
	//2.添加新的座位数据
	//通过演出厅ID获取演出厅
	hall := GetHallByID(schedule.HallID)
	err = AddSeats(hall, schedule.ID)
	if err != nil {
		return err
	}
	return nil
}

// DeleteSchedule 删除演出计划
func DeleteSchedule(scheduleID int64) error {
	DeleteSeats(scheduleID)
	//写 sql 语句
	sqlStr := "delete from schedules where id=?"
	//执行
	_, err := utils.DB.Exec(sqlStr, scheduleID)
	if err != nil {
		return err
	}
	return nil
}

// GetScheduleByDate 根据演出日期查询演出计划
func GetSchedulesByDate(date string) ([]*model.Schedule, error) {
	//写 sql 语句
	sqlStr := "select id, hall_id, film_name, date, start_time, end_time, price from schedules where date=?"
	//执行
	rows, err := utils.DB.Query(sqlStr, date)
	if err != nil {
		return nil, err
	}
	var schedules []*model.Schedule
	for rows.Next() {
		schedule := &model.Schedule{}
		rows.Scan(&schedule.ID, &schedule.HallID, &schedule.FilmName, &schedule.Date, &schedule.StartTime, &schedule.EndTime, &schedule.Price)
		schedules = append(schedules, schedule)
	}
	return schedules, nil
}

// GetScheduleByID 根据演出日期查询演出计划
func GetSchedulesByID(id int64) *model.Schedule {
	//写 sql 语句
	sqlStr := "select id, hall_id, film_name, date, start_time, end_time, price from schedules where id=?"
	//执行
	row := utils.DB.QueryRow(sqlStr, id)
	schedule := &model.Schedule{}
	row.Scan(&schedule.ID, &schedule.HallID, &schedule.FilmName, &schedule.Date, &schedule.StartTime, &schedule.EndTime, &schedule.Price)
	return schedule
}

// GetSchedules 获取所有演出计划
func GetSchedules() ([]*model.Schedule, error) {
	//写 sql 语句
	sqlStr := "select id, hall_id, film_name, date, start_time, end_time, price from schedules"
	//执行
	rows, err := utils.DB.Query(sqlStr)
	if err != nil {
		return nil, err
	}
	var schedules []*model.Schedule
	for rows.Next() {
		schedule := &model.Schedule{}
		rows.Scan(&schedule.ID, &schedule.HallID, &schedule.FilmName, &schedule.Date, &schedule.StartTime, &schedule.EndTime, &schedule.Price)
		schedules = append(schedules, schedule)
	}
	return schedules, nil
}

// GetScheduleByDateAndTime 根据日期时间查找演出计划
func GetScheduleByDateAndTime(schedule0 *model.Schedule) *model.Schedule {
	//写 sql 语句
	sqlStr := "select id, hall_id, film_name, date, start_time, end_time,price from schedules where hall_id=? and (date=?) and ((start_time <= ? and end_time >= ? ) or (start_time <= ? and end_time >= ? ))"
	//执行
	row := utils.DB.QueryRow(sqlStr, schedule0.HallID, schedule0.Date, schedule0.StartTime, schedule0.StartTime, schedule0.EndTime, schedule0.EndTime)
	schedule := &model.Schedule{}
	row.Scan(&schedule.ID, &schedule.HallID, &schedule.FilmName, &schedule.Date, &schedule.StartTime, &schedule.EndTime, &schedule.Price)
	return schedule
}

// GetSchedulesByDateAndFilmName 根据电影名称和时间获取演出计划
func GetSchedulesByDateAndFilmName(date, filmName string) ([]*model.Schedule, error) {
	//写 sql 语句
	sqlStr := "select id, hall_id, film_name, date, start_time, end_time,price from schedules where film_name=? and (date=?)"
	//执行
	rows, err := utils.DB.Query(sqlStr, filmName, date)
	if err != nil {
		return nil, err
	}
	var schedules []*model.Schedule
	for rows.Next() {
		schedule := &model.Schedule{}
		rows.Scan(&schedule.ID, &schedule.HallID, &schedule.FilmName, &schedule.Date, &schedule.StartTime, &schedule.EndTime, &schedule.Price)
		schedules = append(schedules, schedule)
	}
	return schedules, nil
}
